package www.wen.com.common.lib.util

import android.app.Activity
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.annotation.NonNull
import www.wen.com.common.lib.app.BaseApplication

import java.util.*
import kotlin.system.exitProcess


/**
 * @author WEN
 * @Description:
 * @date 2020/11/27 9:38
 */


object ActivityManagerUtil : ActivityManage {

    private val mActivityList = LinkedList<Activity>()

    override fun onActivityDestroyed(activity: Activity) {
        mActivityList.remove(activity)
    }

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        if(!mActivityList.contains(activity)){
            mActivityList.add(activity)
        }
    }

    /**
     * 结束Activity
     * @param activity   The activity.
     * @param isLoadAnim True to use animation for the outgoing activity, false otherwise.
     */
    override fun finishActivity(@NonNull activity: Activity, isLoadAnim: Boolean) {
        activity.finish()
        if (!isLoadAnim) {
            activity.overridePendingTransition(0, 0)
        }
    }

    fun isActivityExistsInStack(@NonNull clz: Class<out Activity?>): Boolean {
        val activities: List<Activity> = mActivityList
        for (activity in activities) {
            if (activity.javaClass == clz) {
                return true
            }
        }
        return false
    }

    fun getActivity(@NonNull clz: Class<out Activity?>): Activity? {
        val activities: List<Activity> = mActivityList
        for (activity in activities) {
            if (activity.javaClass == clz) {
                return activity
            }
        }
        return null
    }

    fun finishActivity(
        @NonNull clz: Class<out Activity?>,
        isLoadAnim: Boolean = false
    ) {
        val activities: List<Activity> = mActivityList
        for (activity in activities) {
            if (activity.javaClass == clz) {
                activity.finish()
                if (!isLoadAnim) {
                    activity.overridePendingTransition(0, 0)
                }
            }
        }
    }

    /**
     * 结束所有Activity并结束应用
     * @param isLoadAnim True to use animation for the outgoing activity, false otherwise.
     */
    override fun finishAllActivities(isLoadAnim: Boolean) {
        for (i in mActivityList.indices.reversed()) { // remove from top
            val activity = mActivityList[i]
            // sActivityList remove the index activity at onActivityDestroyed
            activity.finish()
            if (!isLoadAnim) {
                activity.overridePendingTransition(0, 0)
            }
        }
        mActivityList.clear()
         val activityManage = BaseApplication.CONTEXT.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        activityManage.killBackgroundProcesses(BaseApplication.CONTEXT.packageName)
        exitProcess(0)
    }

    fun finishToActivity(
        @NonNull activityClass: Class<*>,
        isIncludeSelf: Boolean = false,
        isLoadAnim: Boolean = false
    ): Boolean {
        for (i in mActivityList.size - 1 downTo 0) {
            val activity = mActivityList[i]
            if(activity.javaClass == activityClass){
                if(isIncludeSelf){
                    finishActivity(activity, isLoadAnim)
                }
                return true
            }
            finishActivity(activity, isLoadAnim)
        }
        return false
    }

}

interface ActivityManage : Application.ActivityLifecycleCallbacks{

    fun finishActivity(@NonNull activity: Activity, isLoadAnim: Boolean)

    fun finishAllActivities(isLoadAnim: Boolean = false)

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStarted(activity: Activity) {
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
    }

    override fun onActivityStopped(activity: Activity) {
    }

    override fun onActivityResumed(activity: Activity) {
    }

}
